package models;

import java.util.LinkedList;
import java.util.List;
import java.sql.*;
import java.util.Date;

import javax.persistence.*;

import models.Request;

import play.data.validation.*;
import play.db.jpa.Model;

@Entity
public class Request extends Model {
	@Required
	@OneToOne
 private User sender;
	@Required
	@OneToOne
	private User reciever;
	@Required
 private String content;
	@Required
 private Date date;
	@Required
private boolean saveAsDraft;
	@Required
private  String type;
	@Required
private String title;	

private String response;//whether the request accepted or rejected or pending 

@OneToMany (mappedBy = "request", cascade=CascadeType.ALL)
private List<Rating> ratings;

private String averageRating; //changed to string

@Required
@OneToMany (mappedBy = "request",  cascade=CascadeType.ALL)
private List<Comment> comments;      // Added by Arwa Ismail
	
	// Request constructor Author Nora
	// Added the initialization for the comments List in both constructors   Author: Arwa Ismail
	public Request(){
		this.sender = new User();
		this.reciever = new User();
		this.content ="";
		this.date = new Date(new Date().getTime());
		this.saveAsDraft = false;
		this.type ="";
		this.title = "";
		this.response ="pending";
		this.ratings = new LinkedList<Rating>();
		this.comments = new LinkedList<Comment>();
		this.averageRating = "Low";
	}
	//request constructor Author Nora
	public Request (User sender ,User reciever , String content,
							boolean saveAsDraft ,String type) {
		
		this.sender = sender;
		this.reciever = reciever;
		this.content = content;
		this.date= new Date();
		this.title = "";
		this.saveAsDraft = saveAsDraft;
		this.type = type;
		this.response ="pending";
		this.ratings = new LinkedList<Rating>();
		this.comments = new LinkedList<Comment>();
		this.averageRating = "Low";
	}
 
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public String getResponse() {
		return response;
	}
	public void setResponse(String response) {
		this.response = response;
	}
	public User getSender() {
		return sender;
	}

	public void setSenderId(User sender) {
		this.sender = sender;
	}

	public User getReciever() {
		return reciever;
	}

	public void setRecieverId(User reciever) {
		this.reciever = reciever;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public Date getDate() {
		return date;
	}

	public void setDate(Date date) {
		this.date = date;
	}

	public boolean isSaveAsDraft() {
		return saveAsDraft;
	}

	public void setSaveAsDraft(boolean saveAsDraft) {
		this.saveAsDraft = saveAsDraft;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public List<Rating> getRatings() {
		return ratings;
	}
	public void setRatings(List<Rating> ratings) {
		this.ratings = ratings;
	}
	public String getAverageRating() {
		return averageRating;
	}
	public void setAverageRating(String averageRating) {
		this.averageRating = averageRating;
	}
	
	public void setSender(User sender) {
		this.sender = sender;
	}
	public void setReciever(User reciever) {
		this.reciever = reciever;
	}
	public List<Comment> getComments() {
		return comments;
	}
	public void setComments(List<Comment> comments) {
		this.comments = comments;
	}

	
	/**
	 * returns a list of all requests in the system.
	 *
	 * @author      karim khalaf	
	 * @version     2010.0424                            
	 * @category   C3D2
	 * @return       List<Request>
	 */
	 public List<Request> returnAllRequests()  
	    {
	    	List allRequests= Request.findAll();
	    	return allRequests;
	    }
	  
		/**
		 * returns a list of all requests of type "join" in the system.
		 *
		 * @author      karim khalaf	
		 * @version     2010.0424                            
		 * @category   C3D6
		 * @return       List<Request>
		 */
	 public List<Request> returnRequestsOfType() 
	    {
	    	List joinRequests= User.find("byType","join").fetch();
	    	
	    	return joinRequests;
	    }
	 
	 /**
		 * returns a boolean value specifies the response of a specific request either true for acceptance or false for rejection (or pending or notfound).
		 * 
		 * @author wassim.gabriel
		 * @version 2010.0429
		 * @category C3D7
		 * @param requestId
		 * @return Boolean
		 */
	 public boolean requestResponse(long requestId) 
	    {
		 List<Request> requests = Request.findAll();
		 Request specificRequest = new Request();
		 Boolean found = false;
		 for(int i =0;i<requests.size() && !found;i++){
			 if(requests.get(i).id == requestId){
				 specificRequest = requests.get(i);
				 found = true;
			 }
		 }
		 if(found == false){
			 return false;
		 }
	    	if(specificRequest.response.equals("accepted")){
	    		return true;
	    	}else{
	    		return false;
	    	}
	    }
	   
		/**
		 * Calculates the current average rating of the request
		 * 
		 * @author Arwa Ismail
		 * @version 2010.0429
		 * @category C4S10
		 * @return void
		 */

		public void calculateAverageRating() {

			float temp = 0;		// changed to float

			for (int i = 0; i < ratings.size(); i++) {
				if (ratings.get(i).getRating().equalsIgnoreCase("high"))
					temp = temp + 3;

				if (ratings.get(i).getRating().equalsIgnoreCase("medium"))
					temp = temp + 2;

				if (ratings.get(i).getRating().equalsIgnoreCase("low"))
					temp = temp + 1;
			}

			float temp2 = temp / (ratings.size());
			
			if (temp2 < 1.65)
				this.averageRating = "Low";
			else
			{
				if(temp2 < 2.35)
					this.averageRating = "Medium";
					else
						this.averageRating = "High";
			}
			
			
	
		}
	 
	 
	 
}
